package com.cwn.wethink.common.utils.rar;

import net.sf.sevenzipjbinding.*;
import net.sf.sevenzipjbinding.impl.RandomAccessFileOutStream;
import java.io.*;

public class SevenZipServer {
    /**
     * supper tar zip
     * @param filename Compressed file name and path
     * @param compressDir  Wait for compressed files or folder paths
     * @param format The format of the compressed
     */
    public static boolean compressZIP7(String compressDir, String filename, ArchiveFormat format) {
        Item[] items = CompressOutItemStructure.create(compressDir);
        boolean success = false;
        RandomAccessFile raf = null;
        IOutCreateArchive outArchive = null;
        try {
            raf = new RandomAccessFile(filename, "rw");
            outArchive = SevenZip.openOutArchive(format);
            outArchive.createArchive(new RandomAccessFileOutStream(raf),
                    items.length, new Zip7CompressCallBack(items));
            success = true;
        } catch (SevenZipException e) {
            e.printStackTraceExtended();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outArchive != null) {
                try {
                    outArchive.close();
                } catch (IOException e) {
                    success = false;
                }
            }
            if (raf != null) {
                try {
                    raf.close();
                } catch (IOException e) {
                    success = false;
                }
            }
        }
        if (success) {
            System.out.println("Compression operation succeeded");
        }
        return success;
    }

    public static void main(String[] args) {
        compressZIP7("E:\\汇思工作\\新工作文档\\江西农信\\2024年项目文件\\代码不合规","out.rar",ArchiveFormat.RAR);
    }
}